package com.jzx.provider.cv.service;

import com.hankcs.hanlp.HanLP;
import com.jzx.provider.common.utils.BeanUtil;
import com.jzx.provider.common.utils.DateUtil;
import com.jzx.provider.cv.dto.command.EducationInfoCommand;
import com.jzx.provider.cv.model.EducationInfo;
import com.jzx.provider.cv.repository.CvRepository;
import com.jzx.provider.cv.repository.EducationInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author daihuihui
 * @date 2019/2/5
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EducationInfoService {
    @Autowired
    private EducationInfoRepository educationInfoRepository;

    @Autowired
    private CvRepository cvRepository;


    public void saveOrUpdate(EducationInfoCommand educationInfoCommand) {
        EducationInfo educationInfo = EducationInfo.builder().build();
        BeanCopier.create(EducationInfoCommand.class, EducationInfo.class, false)
                .copy(educationInfoCommand, educationInfo, null);
        educationInfoRepository.save(educationInfo);
        //更新简历时间
        cvRepository.findById(educationInfoCommand.getCvId()).ifPresent(cv -> {
            cvRepository.save(cv);
            //更新es索引
        });
    }


    public void deleteEdu(Integer id) {
        educationInfoRepository.findById(id).ifPresent(educationInfo -> {
            educationInfoRepository.deleteById(id);
            //更新简历时间
            cvRepository.findById(educationInfo.getCvId()).ifPresent(cv -> {
                cv.setUpdateTime(new Date());
                cvRepository.save(cv);
                //更新es索引
            });
        });
    }

    public void saveOrUpdateEducationInfo(EducationInfoCommand educationInfoCommand) {
        //处理开始和结束日期
        if (!"至今".equals(educationInfoCommand.getStartDate())) {
            educationInfoCommand.setStartDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(educationInfoCommand.getStartDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
        }
        if (!"至今".equals(educationInfoCommand.getEndDate())) {
            educationInfoCommand.setEndDate(new SimpleDateFormat("yyyy-MM").format(DateUtil.strToDate(educationInfoCommand.getEndDate().replace("Z", " UTC"), "yyyy-MM-dd'T'HH:mm:ss.SSS Z")));
        }
        EducationInfo educationInfo = EducationInfo.builder().build();
        //判断211、985
        HanLP.segment(educationInfoCommand.getSchool()).forEach(term -> {
            if (term.nature.toString().equals("school1")) {
                educationInfo.setIsKeySchool(1);
            } else if (term.nature.toString().equals("school2")) {
                educationInfo.setIsKeySchool(2);
            } else if (term.nature.toString().equals("school")) {
                educationInfo.setIsKeySchool(0);
            } else {
                educationInfo.setIsKeySchool(0);
            }
        });
        if (null != educationInfoCommand.getId()) {
            BeanCopier.create(EducationInfoCommand.class, EducationInfo.class, false).copy(educationInfoCommand, educationInfo, null);
            educationInfo.setUpdateTime(new Date());
            educationInfoRepository.updateCvEducationInfo(educationInfo);
        } else {
            BeanCopier.create(EducationInfoCommand.class, EducationInfo.class, false).copy(educationInfoCommand, educationInfo, null);
            educationInfo.setCreateTime(new Date());
            educationInfo.setUpdateTime(new Date());
            educationInfoRepository.save(educationInfo);
        }
        cvRepository.findById(educationInfo.getCvId()).ifPresent(cv -> {
            cv.setUpdateTime(new Date());
            cv.setIsIndex(0);
            cvRepository.save(cv);
        });
    }
}
